package org.opens.guava.utilites.eventBus.events;

import com.google.common.eventbus.EventBus;
import org.junit.Test;
import org.opens.guava.utilites.eventBus.base.Apple;
import org.opens.guava.utilites.eventBus.base.Fruit;
import org.opens.guava.utilites.eventBus.listeners.ConcreteListener;
import org.opens.guava.utilites.eventBus.listeners.EatFruitListener;

public class EatFruitEventBusExample {

    /**
     * 功能:
     *      测试参数有继承关系时是怎么触发的.
     * 输出:
     *      12:46:49.013 [main] INFO org.opens.guava.utilites.eventBus.listeners.SimpleListener - eat Apple: Apple{name=苹果}
     *      12:46:49.013 [main] INFO org.opens.guava.utilites.eventBus.listeners.SimpleListener - eat Fruit: Apple{name=苹果}
     * 说明:
     *      1. 可以发现, 以事件参数继承树的子类和父类为参数的事件方法都会被触发.
     *          - 这是集合test2方法分析的结果-> 使用父类作为时间参数, 子类的事件监听方法并没有被执行.
     */
    @Test
    public void test1() {
        final EventBus eventBus = new EventBus();
        eventBus.register(new EatFruitListener());
        eventBus.post(new Apple("苹果"));
    }

    /**
     * 输出:
     *      12:50:30.519 [main] INFO org.opens.guava.utilites.eventBus.listeners.SimpleListener - eat Fruit: Fruit{name=苹果}
     * 说明:
     *      请结合test1方法分析.
     */
    @Test
    public void test2() {
        final EventBus eventBus = new EventBus();
        eventBus.register(new EatFruitListener());
        eventBus.post(new Fruit("苹果"));
    }

}
